<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="author" content="贾文清">
    <title>简易轮播图</title>
    <style>
        .box {
            width: 520px;
            height: 280px;
            margin: 100px auto;
            position: relative;
        }

        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        li {
            position: absolute;
            top: 0;
            left: 0;
            display: none;
        }

        li.current {
            display: block;
        }

        .yuan {
            width: 200px;
            height: 20px;
            background-color:  hsla(0,0%,100%,.3);
            border-radius: 12px;
            margin: 0 auto;
             position: relative; 
             top: 250px; 
        }

        .yuan span {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 100%;
            background-color: #fff;
            margin-left: 20px;
        }
        .yuan .active{
            background-color: red;
           
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <!-- (li>a[href=#]>img[src=img/$$.jpg])*5  -->
            <li class="current"><a href="#"><img src="./img/01.jpg" alt=""></a></li>
            <li><a href="#"><img src="./img/02.jpg" alt=""></a></li>
            <li><a href="#"><img src="./img/03.jpg" alt=""></a></li>
            <li><a href="#"><img src="./img/04.jpg" alt=""></a></li>
            <li><a href="#"><img src="./img/05.jpg" alt=""></a></li>
        </ul>
        <div class="yuan">
            <span class="active"></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span> 
        </div>
    </div>
</body>

</html>
<script>
    var spans = document.querySelectorAll('span');
    var lis = document.querySelectorAll('li');
    // 初始 显示索引为0的第一张
    var index = 0;//记录当前显示图片的索引值
    // 每隔1秒钟切换一张图片
    setInterval(changeImage, 1000);
    function changeImage() {
        // 让所有的图片隐藏，去除current类名
        for (var i = 0; i < lis.length; i++) {
            lis[i].className = '';
        }
        for (var j = 0; j < spans.length; j++) {
            spans[j].className = '';
        }
        // 记录显示图片的索引值 加 1
        index++;
        if (index == 5) {
            // 当循环到最后一张图片时从第一张开始
            index = 0;
        }
        console.log(index);
        // 根据索引值显示对应的图片
        lis[index].className = 'current';
        spans[index].className = 'active';
        
    }
    

</script>